package fr.noe.amicaleloisirs.concours.view;

import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CaptionPanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.VerticalPanel;

import fr.noe.amicaleloisirs.concours.business.Player;
import fr.noe.amicaleloisirs.concours.interfaces.CallbackHandler;
import fr.noe.amicaleloisirs.concours.utils.Constants;
import fr.noe.amicaleloisirs.concours.utils.HandlerHelper;
import gwtquery.plugins.droppable.client.events.DropEvent;
import gwtquery.plugins.droppable.client.events.DropEvent.DropEventHandler;
import gwtquery.plugins.droppable.client.gwt.DroppableWidget;

/**
 * 
 * @author Sébastien PREVOST
 *
 */
public class EquipesPanel extends VerticalPanel {
	
	private final Button ajouterEquipe = new Button("Ajouter");
	private final FlexTable flexTable = new FlexTable();
	private final CaptionPanel captionPanel = new CaptionPanel("Liste des équipes");
	private ListeJoueursPanel listeJoueursPanel;
	
	
	public EquipesPanel(ListeJoueursPanel listeJoueursPanel) {
		super();
		
		setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		setStyleName("equipes-panel");
		
		this.listeJoueursPanel = listeJoueursPanel;
		ajouterEquipe.addClickHandler(new ClickHandler() {
			
			public void onClick(ClickEvent event) {
				addNewRow();
			}
		});
		
		captionPanel.setContentWidget(flexTable);
		
		setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		
		ajouterEquipe.getElement().getStyle().setMarginTop(20, Unit.PX);
		
		add(captionPanel);
		add(ajouterEquipe);
		
		for (int i = 0 ; i < Constants.NB_TEXBOX_DEFAUT_CREATION_EQUIPES ; i++) {
			addNewRow();
		}
		
		add(new DemarrerConcoursPanel(this));
	}
	
	public FlexTable getEquipesList() {
		return flexTable;
	}
	
	private void addNewRow() {
		final TextBoxWithData<Player> player1 = new TextBoxWithData<Player>();
		player1.setEnabled(false);
		player1.addStyleName("text-black");
		final TextBoxWithData<Player> player2 = new TextBoxWithData<Player>();
		player2.setEnabled(false);
		player2.addStyleName("text-black");
		DroppableWidget<TextBoxWithData<Player>> dropPlayer1 = new DroppableWidget<TextBoxWithData<Player>>(player1);
		DroppableWidget<TextBoxWithData<Player>> dropPlayer2 = new DroppableWidget<TextBoxWithData<Player>>(player2);
		Image img = new Image("./images/delete_user.png");
		
		int index = flexTable.getRowCount();
		
		HandlerHelper.addMouseOverHandler(img);
		HandlerHelper.addMouseOutHandler(img);
		addClickHandler(img);
		
		img.setTitle("Supprimer cette ligne");
		
		flexTable.setWidget(index, 0, dropPlayer1);
		flexTable.setWidget(index, 1, dropPlayer2);
		flexTable.setWidget(index, 2, img);
		
		
		addDropHandler(dropPlayer1, player1);
		addDropHandler(dropPlayer2, player2);
	}
	
	private void addClickHandler(final Image img) {
		img.addClickHandler(new ClickHandler() {
			
			public void onClick(ClickEvent event) {
				new ConfirmPopUp("Etes-vous sûr(e) de vouloir supprimer cette équipe ?", new CallbackHandler() {
					
					public void onValidate() {
						int index = -1;
						for (int i = 0 ; i < flexTable.getRowCount(); i++) {
							if (flexTable.getWidget(i, 2) == img) {
								index = i;
								break;
							}
						}
						if (index != -1)
							flexTable.removeRow(index);
					}
					
					public void onCancel() {
						// do nothing
					}
				});
			}
		});
	}

	/**
	 * Ajoute un dropHandler sur le droppable en param et affiche le contenu dans la textbox de l'autre param.
	 * 
	 * @param droppable
	 * @param box
	 */
	private void addDropHandler(final DroppableWidget<TextBoxWithData<Player>> droppable, final TextBoxWithData<Player> box) {
		droppable.addDropHandler(new DropEventHandler() {

			public void onDrop(DropEvent event) {
				String stringIndex = event.getDragHelper().getAttribute(Constants.ATTRIBUTE_PLAYER_INDEX);
				int index = -1;
				if (stringIndex != null && !stringIndex.isEmpty()) {
					index = Integer.parseInt(stringIndex);
				}
				Player p = listeJoueursPanel.getPlayerByIndex(index);
				if (p != null) {
					box.setText(p.getFirstname() + " " + p.getLastname());
					box.setData(p);
				}
			}
		});
	}
}
